<form class="form-horizontal" enctype="multipart/form-data"
      style="margin-top: 30px" id="form1">
    <input type="text" name="names" value="1111111111111111111111111111111111111111111111111">
    <div class="form-group">
        <label class="col-12 col-sm-3 control-label">商品广告图：</label>
        <div class="col-3">
            <input type="file" name="images"
                   onchange="selectImage(this);"> <img id="image"
                                                        style="width:375px;height:150px" src="" />
        </div>
        <label class="col-5 control-label"
               style="text-align: left;color: #ff6c29">*必须上传商品banner图片（建议大小为375*150像素）</label>
    </div>
    <div class="col-4 col-offset-2">
        <button onclick="submits()" class="col-4 btn btn-primary"
                type="button">提交</button>
    </div>

</form>
<script>
    var image = '';
    function selectImage(file) { //显示图片
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function(evt) {
            document.getElementById('image').src = evt.target.result;
            image = evt.target.result;
        };
        reader.readAsDataURL(file.files[0]);
    }
    function submits(){
        /*var aarr = document.getElementsByTagName("input");
        var barr = [];
        var picreg = /^[A-Za-z0-9\._]{1,100}$/; //图片名称正则
        var flag1 = false; //用于判断图片大小
        var flag2 = false; //用于判断图片名称
        for (var i = 0; i < aarr.length; i++) {
            if (aarr[i].type == "file") {
                barr.push(aarr[i])
            }
        }*/
        console.log(new FormData($('#form1')[0]))
        $.ajax({
            url : "/admin/main/addBanner",
            type : 'POST',
            cache : false,
            data : new FormData($('#form1')[0]),
            processData : false,
            contentType : false
        }).done(function(res) {
            console.log(res)
            if (res == 1) {
                alert("提交成功")
            }  else if (res == 2) {
                alert("提交失败")
            }
        }).fail(function(res) {
            alert("服务器异常")
        });

    }
</script>